<!--
描述：系统应用-角色管理-权限分配组件
开发人：fengjing
开发日期：2017年12月29日
-->
<template lang="pug">
  kalix-dialog.user-add(
  bizKey="role"
  ref="kalixBizDialog"
  v-bind:formModel.sync="formModel"
  v-bind:targetURL="targetURL"
  v-bind:submitCustom="submitCustom"
  )
    div.el-form(slot="dialogFormSlot" style="max-height:550px;min-height:550px;overflow:auto;border: 1px solid #d0d0d0;margin:-30px 0 -20px")
      el-tree.filter-tree(v-bind:data="treeData" v-bind:props="defaultProps" node-key="id" highlight-current
      show-checkbox v-bind:default-checked-keys="checkedKeys"
      empty-text="数据加载中!" ref="baseTree"
      v-on:check-change="checkChange")
</template>

<script type="text/ecmascript-6">
  import FormModel from './model'
  import {rolesURL} from '../../config.toml'
  import {ON_REFRESH_DATA} from '../../../../../components/corelib/components/common/event.toml'

  export default {
    name: 'AdminRoleAuth',
    data() {
      return {
        formModel: Object.assign({}, FormModel),
        treeDataURL: '/camel/rest/roles',
        treeData: [],
        defaultProps: {
          children: 'children',
          label: 'name'
        },
        checkedKeys: [],
        ids: [],
        funIds: [],
        targetURL: rolesURL,
        contentMessage: ''
      }
    },
    mounted() {
    },
    watch: {
      formModel: {
        handler: 'watchFormModel'
      }
    },
    methods: {
      // 获取数据
      getData() {
        this.treeData = []
        this.checkedKeys = []
        if (!this.treeDataURL) {
          return
        }
        if (!this.formModel.id) {
          return
        }
        let url = this.treeDataURL + '/' + this.formModel.id + '/authorizations'
        this.axios.request({
          method: 'GET',
          url: url,
          params: {}
        }).then(res => {
          if (res.data.children) {
            this.treeData = res.data.children
            // 获取选中节点, 并设置所有节点disable=true
            this.getCheckedKeys(this.treeData)
          }
        })
      },
      // 获取tree组件中被选中结点的id
      getCheckedKeys(array) {
        if (array && array.length > 0) {
          for (let i = 0; i < array.length; i++) {
            if (array[i].children) {
              // array[i].disabled = true
              if (array[i].children.length === 0) {
                // delete array[i].children
                if (array[i].checked) {
                  this.checkedKeys.push(array[i].id)
                }
              } else {
                this.getCheckedKeys(array[i].children)
              }
            }
          }
        }
      },
      close() {
        this.onCancelClick()
      },
      // 封装数据格式
      getIds(array) {
        if (array && array.length > 0) {
          for (let i = 0; i < array.length; i++) {
            if (array[i].children) {
              // array[i].disabled = true
              if (array[i].parentId === -1 && array[i].checked) {
                this.funIds.push('app:' + array[i].id)
              }
              if (array[i].children.length === 0) {
                // delete array[i].children
                if (array[i].checked) {
                  if (array[i].parentId === '-1') {
                    this.funIds.push('app:' + array[i].id)
                  } else {
                    this.funIds.push('fun:' + array[i].id)
                  }
                }
              } else {
                if (array[i].checked) {
                  this.funIds.push('fun:' + array[i].id)
                }
                this.getIds(array[i].children)
              }
            }
          }
        }
      },
      checkChange(a, b, c) {
        // console.log('currentChange a:', a)
        // console.log('currentChange b:', b)
        // console.log('currentChange c:', c)
        // if (b) {
        //   a.checked = true
        // } else if (c) {
        //   a.checked = true
        // } else {
        //   a.checked = false
        // }
      },
      watchFormModel(newVal) {
        this.getData()
        if (newVal.id) {
          this.targetURL = this.targetURL + '/' + newVal.id + '/authorizations'
        }
        this.ids.push(newVal.id)
      },
      // 重写basedialog的提交方法
      submitCustom(baseDialog) {
        this.funIds.push('fun:root')
        // console.group('%c ===== Role ===== ', 'background:#550000;color:#ffffff;')
        // console.log('this.treeData', this.treeData)
        // this.getIds(this.treeData)
        let treeNodes = this.$refs.baseTree.getCheckedNodes()
        if (treeNodes.length) {
          let isolatedNode = treeNodes.filter(e => {
            return e.children.length === 0
          })
          if (isolatedNode.length) {
            console.log('isolatedNode', isolatedNode)
            this.funA(isolatedNode)
          }
        }
        this.ids.push(this.funIds.join(','))
        /*
        */
        baseDialog.$refs.dialogForm.validate((valid) => {
          if (valid) {
            this.axios.request({
              method: baseDialog.isEdit ? 'PUT' : 'POST',
              url: baseDialog.isEdit ? `${this.targetURL}/${this.formModel.id}` : this.targetURL,
              data: this.ids,
              params: {}
            }).then(response => {
              if (response.data.success) {
                this.$KalixMessage.success(response.data.msg)
                baseDialog.visible = false
                baseDialog.$refs.dialogForm.resetFields()
                baseDialog.submitComplete()
              } else {
                this.$KalixMessage.error(response.data.msg)
                baseDialog.submitComplete()
              }
              // 刷新列表
              this.$KalixEventBus.$emit(ON_REFRESH_DATA)
              baseDialog._afterDialogClose()
              console.log('[kalix] dialog submit button clicked !')
              baseDialog.visible = false
            })
          } else {
            this.$KalixMessage.error('请检查输入项！')
            baseDialog.submitComplete()
            return false
          }
        })
        // console.log('this.$refs.baseTree:', this.$refs.baseTree.getCheckedNodes())
        // console.log('this.funIds', this.funIds)
        // console.log('ids', this.ids)
        this.ids = []
        // console.groupEnd()
      },
      funA(_isolatedNode) {
        this.myNodes = {}
        this.takeNodes(this.treeData)
        // console.log('this.treeData str:', JSON.stringify(this.treeData))
        // console.log('this.myNodes:', this.myNodes)
        // let funs = []
        // let maps = {}
        // _isolatedNode.forEach(e => {
        //   // e.parentId
        //   if (e.parentId > -1) {
        //     maps[e.parentId] = e.parentId
        //   }
        //   funs.push(`fun:${e.id}`)
        // })
        // console.log('MAPS', maps)
        // this.treeData.
        // for (let key in maps) {
        // }
        // this.treeData.
        let arrFunA = {}
        _isolatedNode.forEach(e => {
          arrFunA[e.parentId] = this.myNodes[e.parentId]
          this.funIds.push(`fun:${e.id}`)
        })
        console.log('arrFunA', arrFunA)
        let arrFunB = {}
        for (let key in arrFunA) {
          arrFunB[arrFunA[key].parentId] = this.myNodes[arrFunA[key].parentId]
          this.funIds.push(`fun:${arrFunA[key].id}`)
        }
        console.log('arrB', arrFunB)
        let arrApp = {}
        for (let key in arrFunB) {
          arrApp[arrFunB[key].parentId] = this.myNodes[arrFunB[key].parentId]
          this.funIds.push(`fun:${arrFunB[key].id}`)
        }
        console.log('arrApp', arrApp)
        for (let key in arrApp) {
          this.funIds.push(`app:${arrApp[key].id}`)
        }
      },
      takeNodes(_array) {
        if (_array && _array.length > 0) {
          _array.forEach(e => {
            this.myNodes[e.id] = e
            if (e.children.length) {
              this.takeNodes(e.children)
            }
          })
        }
      }
    }
  }
</script>
